package org.yookiee.support.core.serializers;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.google.common.base.Strings;
import org.yookiee.support.core.EasyLocale;
import org.yookiee.support.core.EasyTimeZone;
import org.yookiee.support.core.I18NContext;

import java.io.IOException;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

/**
 * company -
 * FileName CustomLocalDateTimeSerializer
 * Package org.yookiee.support.core.serializers
 * Description
 * author cpstory
 * create 2023-11-15 13:49
 * version V1.0
 */
public class CustomLocalTimeSerializer extends JsonSerializer<LocalTime> {

    private DateTimeFormatter formatter;

    public CustomLocalTimeSerializer() {
    }
    public CustomLocalTimeSerializer(DateTimeFormatter formatter) {
        super();
        this.formatter = formatter;
    }

    @Override
    public void serialize(LocalTime value, JsonGenerator generator, SerializerProvider provider) throws IOException {
        System.out.println("开始进行序列化");
        EasyLocale locale = I18NContext.get();
        if(!Strings.isNullOrEmpty(locale.getTimeZone())){
            EasyTimeZone timeZone = EasyTimeZone.formatByTimeZone(locale.getTimeZone());
            assert timeZone != null;
            generator.writeString(convertLocalDateTime(value, ZoneId.systemDefault(),ZoneId.of(timeZone.getTimezone()) )
                    .format(formatter));
        }else{
            generator.writeString(convertLocalDateTime(value, ZoneId.systemDefault(), ZoneId.systemDefault())
                    .format(formatter));
        }

    }

    /**
     * 时区转换
     *
     * @param localTime
     * @param originZoneId
     * @param targetZoneId
     * @return
     */
    public static LocalTime convertLocalDateTime(LocalTime localTime, ZoneId originZoneId,
                                                     ZoneId targetZoneId) {
        return localTime;
    }

}
